package com.yuexunit.mobilenurse.module.BloodManage.ui;

import android.content.Context;
import android.telephony.TelephonyManager;
import android.view.View;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

import com.yuexunit.mobilenurse.R;
import com.yuexunit.mobilenurse.config.AppConfig;
import com.yuexunit.mobilenurse.module.BloodManage.adapter.BloodBagStateAdapter;
import com.yuexunit.mobilenurse.module.BloodManage.bean.Blood;
import com.yuexunit.mobilenurse.module.BloodManage.bean.BloodBag;
import com.yuexunit.mobilenurse.module.BloodManage.bean.BloodBagDataBean;
import com.yuexunit.mobilenurse.module.BloodManage.bean.CheckDataBean;
import com.yuexunit.mobilenurse.module.BloodManage.callback.IReceiveBloodCallBack;
import com.yuexunit.mobilenurse.module.BloodManage.model.impl.BloodBagApplyModel;
import com.yuexunit.mobilenurse.module.BloodManage.model.impl.ReceiveModel;
import com.yuexunit.mobilenurse.module.BloodManage.presenter.IBloodBagApplyPresenter;
import com.yuexunit.mobilenurse.module.BloodManage.presenter.IReceivePresenter;
import com.yuexunit.mobilenurse.module.BloodManage.presenter.impl.BloodBagApplyPresenter;
import com.yuexunit.mobilenurse.module.BloodManage.presenter.impl.ReceivePresenter;
import com.yuexunit.mobilenurse.module.BloodManage.ui.view.IBloodBagApplyView;
import com.yuexunit.mobilenurse.module.BloodManage.ui.view.IReceiveView;
import com.yuexunit.mobilenurse.util.CreateJson;
import com.yuexunit.mobilenurse.widget.LoadingDialog;

import org.kymjs.kjframe.KJActivity;
import org.kymjs.kjframe.ui.ViewInject;
import org.kymjs.kjframe.utils.PreferenceHelper;
import org.kymjs.kjframe.utils.StringUtils;
import org.kymjs.kjframe.utils.SystemTool;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by wangt on 2017/9/7.
 */

public class Act_Blood_Detail extends KJActivity implements IReceiveView ,IBloodBagApplyView,IReceiveBloodCallBack{
    @Bind(R.id.tv_doc_share_set)
    TextView tvDocShareSet;
    @Bind(R.id.tv_name)
    TextView tvName;
    @Bind(R.id.tv_department)
    TextView tvDepartment;
    @Bind(R.id.tv_hos_num)
    TextView tvHosNum;
    @Bind(R.id.tv_blood_num)
    TextView tvBloodNum;
    @Bind(R.id.tv_bed_num)
    TextView tvBedNum;
    @Bind(R.id.tv_require_doctor)
    TextView tvRequireDoctor;
    @Bind(R.id.tv_require_doctor_num)
    TextView tvRequireDoctorNum;
    @Bind(R.id.bloodlist)
    ListView bloodlist;

    LoadingDialog dialog;
    String requireNo;
    IReceivePresenter presenter;
    IBloodBagApplyPresenter presenter_blood;
    //记录Log
    private final Logger log = LoggerFactory.getLogger(Act_Blood_Detail.class);
    public static final int DOWNLOAD_NETERROR = -1;
    BaseAdapter adapter;

    Blood blood = new Blood();
    ArrayList<BloodBag> bloodBags = new ArrayList<>();

    int position;
    String bloodBag;
    String time;

    @Override
    public void setRootView() {
        setContentView(R.layout.act_blood_detail);
        ButterKnife.bind(this);
    }

    @Override
    public void initData() {
        super.initData();
        presenter = new ReceivePresenter(new ReceiveModel(), this);
        presenter_blood = new BloodBagApplyPresenter(new BloodBagApplyModel(),this);
        requireNo = getIntent().getExtras().getString("requireNo");
        dialog = new LoadingDialog(this);
    }

    @Override
    public void onResume() {
        super.onResume();
        if(SystemTool.checkNet(aty)) {
            Map<String, String> params = new HashMap<>();
            params.put("requireNo",requireNo);
            presenter_blood.getBloodBagDataList(params);
        }
        else
        {
            ViewInject.toast("网络异常，请检查网络是否连接!");
            log.error(CreateJson.LOG_JSON("", "1", PreferenceHelper.readString(aty, AppConfig.NURSE_INFO, AppConfig.NURSE_DEPTNO), ""));
        }
    }

    @OnClick({R.id.back, R.id.tv_doc_share_set})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.back:
                finish();
                break;
            case R.id.tv_doc_share_set:
                break;
        }
    }

    @Override
    public void receiveBlood(CheckDataBean checkDataBean) {
        if (checkDataBean.getStatus().equals("200")) {
            ViewInject.toast("接血成功");
//            tvDocShareSet.setVisibility(View.INVISIBLE);
//            finish();
            bloodBags.get(position).setReceiveJobNum(PreferenceHelper.readString(aty, AppConfig.NURSE_INFO, AppConfig.NURSE_LOGID));
            bloodBags.get(position).setReceiveName(PreferenceHelper.readString(aty, AppConfig.NURSE_INFO, AppConfig.NURSE_LOGNAME));
            bloodBags.get(position).setReceiveTime(time);
            adapter.notifyDataSetChanged();
        } else {
            ViewInject.toast("接血失败");
        }
    }

    @Override
    public void loadingDialogStatus(int status) {
        switch (status) {
            case AppConfig.SHOW_DIALOG:
                dialog.setCanceledOnTouchOutside(false);
                dialog.show();
                break;
            case AppConfig.DISMISS_DIALOG:
                dialog.dismiss();
                break;

        }
    }

    @Override
    public void showBloodBagDataList(BloodBagDataBean bloodBagDataBean) {
        if(bloodBagDataBean.getStatus().equals("200")) {
            tvName.setText( bloodBagDataBean.getDatas().get(0).getPatientName());
            tvDepartment.setText( bloodBagDataBean.getDatas().get(0).getRequireDeptName());
            tvHosNum.setText( bloodBagDataBean.getDatas().get(0).getVisitNo());
            tvBloodNum.setText( bloodBagDataBean.getDatas().get(0).getRequireNo());
            tvBedNum.setText( bloodBagDataBean.getDatas().get(0).getBedNo());
            tvRequireDoctor.setText( bloodBagDataBean.getDatas().get(0).getRequireHisJobName());
            tvRequireDoctorNum.setText( bloodBagDataBean.getDatas().get(0).getRequireHisJobNum());

//            if(bloodBagDataBean.getDatas().get(0).getStatus()==1)
//            {
//                tvDocShareSet.setVisibility(View.VISIBLE);
//            }
            blood = bloodBagDataBean.getDatas().get(0);
            bloodBags = bloodBagDataBean.getDatas().get(0).getBagList();
            adapter = new BloodBagStateAdapter(bloodlist,blood,bloodBags,this, R.layout.item_blood_bag_state);
            bloodlist.setAdapter(adapter);
        }
        else {
            ViewInject.toast("获取数据失败");
            finish();
        }
    }

    @Override
    public void PullDownRefreshState(int state) {

    }

    @Override
    public void callBack(int position, String bagno) {
        this.position = position;
        bloodBag = bagno;
        receiveBlood();
    }

    private void receiveBlood()
    {
        Map<String, String> params = new HashMap<>();
        params.put("bagNo", bloodBag);
        params.put("hisJobNum", PreferenceHelper.readString(aty, AppConfig.NURSE_INFO, AppConfig.NURSE_LOGID));
        params.put("jobName", PreferenceHelper.readString(aty, AppConfig.NURSE_INFO, AppConfig.NURSE_LOGNAME));
        time = StringUtils.getDataTime("yyyy-MM-dd HH:mm:ss");
        params.put("receiveTime", time);
        TelephonyManager telephonyManager = (TelephonyManager) aty
                .getSystemService(Context.TELEPHONY_SERVICE);
        params.put("setNo", telephonyManager.getDeviceId());
        presenter.receiveBlood(params);
    }

}
